/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.ArrayList;
import model.config.MySQLQuery;
import model.pojo.*;
import model.util.MySQLProviders;
/**
 *
 * @author Admin
 */
public class NhatKyTichDiemDAO {
    
    public static ArrayList<NhatKyTichDiemPOJO> LoadDanhSach(String TenDangNhap)
    {
        ArrayList<NhatKyTichDiemPOJO> nktd = new ArrayList<NhatKyTichDiemPOJO>();
        String sql = "Select * From NhatKyTichDiem where TenDangNhap='"+TenDangNhap+"'";
        MySQLProviders helper = new MySQLProviders();
        helper.open();
        ResultSet rs = helper.executeQuery(sql);
        try {
            while(rs.next())
            {
                NhatKyTichDiemPOJO temp = new NhatKyTichDiemPOJO();
                temp.setMaNhatKyTichDiem(rs.getInt("MaNhatKyTichDiem"));
                temp.setSuKien(rs.getBoolean("SuKiem"));
                temp.setThoiGian(rs.getTimestamp("ThoiGian"));
                temp.setDiem(rs.getInt("Diem"));
                temp.setTenDangNhap(TenDangNhap);
                
                nktd.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(NhatKyTichDiemDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            helper.close();
        }
        return nktd;
    }
    
     public static ArrayList<NhatKyTichDiemPOJO> getDanhSachCoPhanTrang(int trang) throws FileNotFoundException, IOException {
      ArrayList<NhatKyTichDiemPOJO> ds = new ArrayList<NhatKyTichDiemPOJO>(); 
      int soSanPhamTrenTrang = ThamSoDAO.laySoLuongTrenTrang();
      int batDau = (trang - 1) * soSanPhamTrenTrang;
      if(soSanPhamTrenTrang==batDau)
          batDau=1;     
      //mo ket noi
      MySQLProviders helper = new MySQLProviders();
      helper.open();
      
      //doc query tu file text
      //lay cau truy van tu file text
     MySQLQuery mySQLQuery=new MySQLQuery();
     mySQLQuery.Read("PHANTRANG");
     String sql=String.format(mySQLQuery.GetQuery(MySQLQuery.QueryKey.SELECT_ALL_NHATKYTICHLUYDIEM_PHANTRANG),batDau,soSanPhamTrenTrang);
      ResultSet rs = helper.executeQuery(sql);
      try {
         while (rs.next()) {
            NhatKyTichDiemPOJO temp = new NhatKyTichDiemPOJO();
            temp.setMaNhatKyTichDiem(rs.getInt("MaNhatKyTichDiem"));
            temp.setSuKien(rs.getBoolean("SuKiem"));
            temp.setThoiGian(rs.getTimestamp("ThoiGian"));
            temp.setDiem(rs.getInt("Diem"));
            temp.setTenDangNhap(rs.getString("TenDangNhap"));
            ds.add(temp);
         }
      } catch (SQLException ex) {
         System.out.println(ex.getMessage());
      }
      helper.close();
      return ds;
   }
     
      public static NhatKyTichDiemPOJO getById(int id){
       NhatKyTichDiemPOJO obj=new NhatKyTichDiemPOJO();
       MySQLProviders helper = new MySQLProviders();
       helper.open();
       ResultSet rs = helper.executeQuery("select *from nhatkytichdiem where manhatkytichdiem="+id);
       try {
           while (rs.next()) {            
            obj.setMaNhatKyTichDiem(rs.getInt("MaNhatKyTichDiem"));
            obj.setSuKien(rs.getBoolean("SuKiem"));
            obj.setThoiGian(rs.getTimestamp("ThoiGian"));
            obj.setDiem(rs.getInt("Diem"));
            obj.setTenDangNhap(rs.getString("TenDangNhap"));
            
         }
       }catch(Exception e){
           
       }finally{
           helper.close();
       }
       return obj;
   }
}
